package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.RealTimeMtBean;
import com.xianbeiying.app.ckgl.widght.AppManager;
import com.xianbeiying.app.ckgl.widght.ChartPieView;

/**
 * 实时监控数据适配
 */
public class ReTimeMontiorAdapter extends BaseAdapter {
    private Context context;
    //    //    private int [] number={0,20,35,65,80};
//    public static int[] COLORS = new int[]{R.color.retime_montior_j, R.color.retm_msx};
//    private double value[] = {38, 62};
//    private String d[] = {"0", "1"};
    private RealTimeMtBean realTimeMtBean;
    private String imgUrl;
    public ReTimeMontiorAdapter(Context context, RealTimeMtBean realTimeMtBean,String imgUrl) {
        this.context = context;
        this.imgUrl=imgUrl;
        this.realTimeMtBean = realTimeMtBean;
    }

    @Override
    public int getCount() {
        return realTimeMtBean.getV().getCROPS().size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_retime_mointor, parent, false);
            holder = new ViewHolder();
            holder.img_retm_plant = (ImageView) convertView.findViewById(R.id.img_retm_plant);
            holder.tv_retimem_date = (TextView) convertView.findViewById(R.id.tv_retimem_date);
            holder.ly_rm_br = (LinearLayout) convertView.findViewById(R.id.ly_rm_br);
            holder.tv_rmsz_br = (TextView) convertView.findViewById(R.id.tv_rmsz_br);
            holder.tv_rmkh_br = (TextView) convertView.findViewById(R.id.tv_rmkh_br);
            holder.tv_rmjg_br = (TextView) convertView.findViewById(R.id.tv_rmjg_br);
            holder.tv_rmcs_br = (TextView) convertView.findViewById(R.id.tv_rmcs_br);
            holder.ly_rm_line = (LinearLayout) convertView.findViewById(R.id.ly_rm_line);

            holder.tv_rmsz_line = (TextView) convertView.findViewById(R.id.tv_rmsz_line);
            holder.tv_rmkh_line = (TextView) convertView.findViewById(R.id.tv_rmkh_line);
            holder.tv_rmjg_line = (TextView) convertView.findViewById(R.id.tv_rmjg_line);
            holder.tv_rmcs_line = (TextView) convertView.findViewById(R.id.tv_rmcs_line);
            holder.tv_plantstart = (TextView) convertView.findViewById(R.id.tv_plantstart);
            holder.tv_sz_number = (TextView) convertView.findViewById(R.id.tv_sz_number);
            holder.tv_kh_number = (TextView) convertView.findViewById(R.id.tv_kh_number);
            holder.tv_jg_number = (TextView) convertView.findViewById(R.id.tv_jg_number);
            holder.tv_cs_number = (TextView) convertView.findViewById(R.id.tv_cs_number);

            holder.retm_line1 = (View) convertView.findViewById(R.id.retm_line1);
            holder.tv_downtime=(TextView) convertView.findViewById(R.id.tv_downtime);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.retm_line1.setVisibility(View.GONE);
        if (position == 0) {
            holder.retm_line1.setVisibility(View.VISIBLE);
        }
//        int sizes = realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS()==null?0:realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().size();
//        holder.tv_rmsz_br.setVisibility(View.GONE);
//        holder.tv_rmkh_br.setVisibility(View.GONE);
//        holder.tv_rmjg_br.setVisibility(View.GONE);
//        holder.tv_rmcs_br.setVisibility(View.GONE);
//        holder.tv_rmsz_line.setVisibility(View.GONE);
//        holder.tv_rmkh_line.setVisibility(View.GONE);
//        holder.tv_rmjg_line.setVisibility(View.GONE);
//        holder.tv_rmcs_line.setVisibility(View.GONE);
//        holder.tv_sz_number.setVisibility(View.GONE);
//        holder.tv_kh_number.setVisibility(View.GONE);
//        holder.tv_jg_number.setVisibility(View.GONE);
//        holder.tv_cs_number.setVisibility(View.GONE);
//        holder.ly_sc.setVisibility(View.GONE);
//        holder.ly_kh.setVisibility(View.GONE);
//        holder.ly_jg.setVisibility(View.GONE);
//        holder.ly_cs.setVisibility(View.GONE);
//        if(sizes == 0) {
//            holder.tv_rmsz_br.setVisibility(View.VISIBLE);
//            holder.tv_rmsz_line.setVisibility(View.VISIBLE);
//            holder.tv_sz_number.setVisibility(View.VISIBLE);
//            holder.ly_sc.setVisibility(View.VISIBLE);
//            holder.ly_kh.setVisibility(View.VISIBLE);
//            holder.ly_jg.setVisibility(View.VISIBLE);
//            holder.ly_cs.setVisibility(View.VISIBLE);
//            holder.tv_cs_time.setText("植物生长周期:" +0+ "天");
//            holder.tv_retimem_date.setText(DataUtil.paserTime(realTimeMtBean.getV().getCROPS().get(position).getPLANTTIME() + "", "yyyy-MM-dd HH:mm"));
////            holder.img_retm_plant.setImageResource(R.mipmap.img_default1);
//            ImageLoader.getInstance().displayImage("http://"+ MyApplication.IP+ UrlBean.IMAGES+imgUrl,holder.img_retm_plant);
//            int[] COLORS = new int[]{R.color.retime_montior_j,R.color.retm_msx};
//            double value[] ={0,100};
//            String d[] = {"0", "1"};
//            holder.chartview.drawChartView(value, d, COLORS);
//            holder.tv_plantstart.setText("0");
//            holder.tv_rm_type.setText("种植/成熟");
//            holder.tv_rm_number.setText("");
//            holder.tv_sz_number.setText("0");
//            holder.tv_rmsz_br.setBackgroundColor(ContextCompat.getColor(context,R.color.retm_msx));
//        } else if (sizes == 1){
//            holder.tv_rmsz_br.setVisibility(View.VISIBLE);
//            holder.tv_rmsz_line.setVisibility(View.VISIBLE);
//            holder.tv_sz_number.setVisibility(View.VISIBLE);
//            holder.ly_sc.setVisibility(View.VISIBLE);
//            holder.ly_kh.setVisibility(View.VISIBLE);
//            holder.ly_jg.setVisibility(View.VISIBLE);
//            holder.ly_cs.setVisibility(View.VISIBLE);
//            int day = realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration();
//            holder.tv_cs_time.setText("植物生长周期:" + day + "天");
//            holder.tv_retimem_date.setText(DataUtil.paserTime(realTimeMtBean.getV().getCROPS().get(position).getPLANTTIME() + "", "yyyy-MM-dd HH:mm"));
////            holder.img_retm_plant.setImageResource(R.mipmap.img_default1);
//            ImageLoader.getInstance().displayImage("http://"+ MyApplication.IP+ UrlBean.IMAGES+imgUrl,holder.img_retm_plant);
//            String startTime= holder.tv_retimem_date.getText().toString();
//            setwidth(holder.tv_downtime,Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime())),day);
//            int[] COLORS = new int[]{R.color.retime_montior_j,R.color.retm_msx};
//            double value[] = {100.00/day*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime())), 100.00-(100.00/day)*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime()))};
//            String d[] = {"0", "1"};
//            holder.chartview.drawChartView(value, d, COLORS);
//            double db=100.00/day*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime()));
//            int jingdu=(int)Math.ceil(db);
//            holder.tv_rm_number.setText(jingdu+"%");
//            setwidth(holder.tv_rmsz_br, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration(), day);
//            setwidth(holder.tv_rmsz_line, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration(), day);
//            holder.tv_plantstart.setText("0");
//            holder.tv_rm_type.setText("种植/成熟");
//            holder.tv_sz_number.setText(realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration() + "");
//        } else {
//            holder.tv_rmsz_br.setVisibility(View.VISIBLE);
//            holder.tv_rmkh_br.setVisibility(View.VISIBLE);
//            holder.tv_rmjg_br.setVisibility(View.VISIBLE);
//            holder.tv_rmcs_br.setVisibility(View.VISIBLE);
//            holder.tv_rmsz_line.setVisibility(View.VISIBLE);
//            holder.tv_rmkh_line.setVisibility(View.VISIBLE);
//            holder.tv_rmjg_line.setVisibility(View.VISIBLE);
//            holder.tv_rmcs_line.setVisibility(View.VISIBLE);
//            holder.tv_sz_number.setVisibility(View.VISIBLE);
//            holder.tv_kh_number.setVisibility(View.VISIBLE);
//            holder.tv_jg_number.setVisibility(View.VISIBLE);
//            holder.tv_cs_number.setVisibility(View.VISIBLE);
//            holder.ly_sc.setVisibility(View.VISIBLE);
//            holder.ly_kh.setVisibility(View.VISIBLE);
//            holder.ly_jg.setVisibility(View.VISIBLE);
//            holder.ly_cs.setVisibility(View.VISIBLE);
//            holder.tv_rm_type.setText("种植/成熟");
//            int day = realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration() + realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(1).getStepDuration() +
//                    realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(2).getStepDuration() + realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(3).getStepDuration();
//            holder.tv_rm_number.setText(realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration() + "天");
//            int[] COLORS = new int[]{R.color.retime_montior_j,R.color.retm_msx};
//            holder.tv_cs_time.setText("植物成熟时间:" + day + "天");
//            holder.tv_retimem_date.setText(DataUtil.paserTime(realTimeMtBean.getV().getCROPS().get(position).getPLANTTIME() + "", "yyyy-MM-dd HH:mm"));
////            holder.img_retm_plant.setImageResource(R.mipmap.img_default1);
//            ImageLoader.getInstance().displayImage("http://"+ MyApplication.IP+ UrlBean.IMAGES+imgUrl,holder.img_retm_plant);
//            String startTime= holder.tv_retimem_date.getText().toString();
//            setwidth(holder.tv_downtime,Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime())),day);
//            double value[] = {100.00/day*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime())), 100.00-(100.00/day)*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime()))};
////            double value[] = {50, 50};
//            double db=100.00/day*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime()));
//            int jingdu=(int)Math.ceil(db);
//            holder.tv_rm_number.setText(jingdu+"%");
//            LogUtils.e((100.00/day)*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime()))+"---",100.00-((100.00/day)*Integer.valueOf(DataUtil.getHHLong(startTime,DataUtil.dateHTime())))+"=======");
//            String d[] = {"0", "1"};
//            holder.chartview.drawChartView(value, d, COLORS);
//            setwidth(holder.tv_rmsz_br, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration(), day);
//            setwidth(holder.tv_rmkh_br, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(1).getStepDuration(), day);
//            setwidth(holder.tv_rmjg_br, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(2).getStepDuration(), day);
//            setwidth(holder.tv_rmcs_br, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(3).getStepDuration(), day);
//            setwidth(holder.tv_rmsz_line, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration(), day);
//            setwidth(holder.tv_rmkh_line, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(1).getStepDuration(), day);
//            setwidth(holder.tv_rmjg_line, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(2).getStepDuration(), day);
//            setwidth(holder.tv_rmcs_line, realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(3).getStepDuration(), day);
//            holder.tv_plantstart.setText("0");
//            int d2 = realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration() + realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(1).getStepDuration();
//            int d3 = realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration() + realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(1).getStepDuration() + realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(2).getStepDuration();
//            holder.tv_sz_number.setText(realTimeMtBean.getV().getCROPS().get(position).getCROPSTEPINFOS().get(0).getStepDuration() + "");
//            holder.tv_kh_number.setText(d2 + "");
//            holder.tv_jg_number.setText(d3 + "");
//            holder.tv_cs_number.setText(day + "");

//        }
        return convertView;
    }

    private class ViewHolder {
        //图片
        ImageView img_retm_plant;
        //时间
        TextView tv_retimem_date;
        //成熟时间
        TextView tv_cs_time;
        //扇形图
        ChartPieView chartview;
        //天数
        TextView tv_rm_number;
        //类型
        TextView tv_rm_type;
        //进度布局
        LinearLayout ly_rm_br;
        TextView tv_rmsz_br;
        TextView tv_rmkh_br;
        TextView tv_rmjg_br;
        TextView tv_rmcs_br;
        //线条布局
        LinearLayout ly_rm_line;
        TextView tv_rmsz_line;
        TextView tv_rmkh_line;
        TextView tv_rmjg_line;
        TextView tv_rmcs_line;
        //进度数字
        TextView tv_plantstart;
        TextView tv_sz_number;
        TextView tv_kh_number;
        TextView tv_jg_number;
        TextView tv_cs_number;
        //线条
        View retm_line, retm_line1;
        LinearLayout ly_sc, ly_kh, ly_jg, ly_cs;
        //当前的时间点
        TextView tv_downtime;
    }

    private void setwidth(TextView textView, int fz, int fm) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) textView.getLayoutParams(); //取控件textView当前的布局参数
        int width = AppManager.getInstance().getWindowSizeEntity().getWidth();
        linearParams.width = width / fm * fz;
        textView.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
    }
}
